|
1
|
|
|
import chai from 'chai'; |
|
2
|
|
|
import chaiaspromised from 'chai-as-promised'; |
|
3
|
|
|
import mockfs from 'mock-fs'; |
|
4
|
|
|
import sinon from 'sinon'; |
|
5
|
|
|
import sinonchai from 'sinon-chai'; |
|
6
|
|
|
import mute from 'mute'; |
|
7
|
|
|
|
|
8
|
|
|
import cgapi from '../src/codingame-api.js'; |
|
9
|
|
|
import configure from '../src/configure.js'; |
|
10
|
|
|
|
|
11
|
|
|
import utils from '../src/utils.js'; |
|
12
|
|
|
|
|
13
|
|
|
let expect = chai.expect; |
|
14
|
|
|
chai.use(chaiaspromised); |
|
15
|
|
|
chai.use(sinonchai); |
|
16
|
|
|
|
|
17
|
|
|
describe(`[module] utils`, function() { |
|
18
|
|
|
describe(`[method] kill`, function() { |
|
19
|
|
|
let sandbox; |
|
20
|
|
|
beforeEach(function() { |
|
21
|
|
|
sandbox = sinon.sandbox.create(); |
|
22
|
|
|
}); |
|
23
|
|
|
afterEach(function() { |
|
24
|
|
|
sandbox.restore(); |
|
25
|
|
|
}); |
|
26
|
|
|
it(`should call process.exit with an negative value`, mute(function() { |
|
27
|
|
|
let exit = sandbox.stub(process, `exit`); |
|
28
|
|
|
let message = `Some error message`; |
|
29
|
|
|
utils.kill(new Error(message)); |
|
30
|
|
|
expect(exit).to.have.been.calledOnce; |
|
31
|
|
|
expect(exit.getCall(0).args[0]).to.be.below(0); |
|
32
|
|
|
})); |
|
33
|
|
|
}); |
|
34
|
|
|
describe(`[method] login`, function() { |
|
35
|
|
|
let sandbox; |
|
36
|
|
|
beforeEach(function() { |
|
37
|
|
|
sandbox = sinon.sandbox.create(); |
|
38
|
|
|
}) |
|
39
|
|
|
afterEach(function() { |
|
40
|
|
|
sandbox.restore(); |
|
41
|
|
|
}); |
|
42
|
|
|
it(`should resolve if login is successfull`, function() { |
|
43
|
|
|
let login = sinon.stub(cgapi, `login`, function() {return Promise.resolve(true)}); |
|
44
|
|
|
let get = sinon.stub(configure, `get`, function(property) {return Promise.resolve(property);}); |
|
45
|
|
|
let log = utils.login(`username`, `password`) |
|
46
|
|
|
let calls = log.then(function() { |
|
47
|
|
|
expect(login).to.have.been.calledOnce; |
|
48
|
|
|
expect(get).to.have.been.calledTwice; |
|
49
|
|
|
return Promise.resolve(true); |
|
50
|
|
|
}); |
|
51
|
|
|
return Promise.all([ |
|
52
|
|
|
expect(log).to.eventually.be.fullfilled, |
|
53
|
|
|
calls |
|
54
|
|
|
]); |
|
55
|
|
|
}); |
|
56
|
|
|
it(`should reject after 3 tries if authentication failed`, mute(function() { |
|
57
|
|
|
let login = sandbox.stub(cgapi, `login`, function() { |
|
58
|
|
|
return Promise.resolve({ |
|
59
|
|
|
"error": new Error(`Cannot authenticate`) |
|
60
|
|
|
}); |
|
61
|
|
|
}); |
|
62
|
|
|
get = sandbox.stub(configure, `get`, function(property) {return Promise.resolve(property);}); |
|
63
|
|
|
let log = utils.login(`username`, `password`) |
|
64
|
|
|
let calls = log.catch(function() { |
|
65
|
|
|
expect(login).to.have.been.calledThrice; |
|
66
|
|
|
return Promise.resolve(true); |
|
67
|
|
|
}); |
|
68
|
|
|
return Promise.all([ |
|
69
|
|
|
expect(log).to.eventually.be.rejected, |
|
70
|
|
|
calls |
|
71
|
|
|
]); |
|
72
|
|
|
})); |
|
73
|
|
|
}); |
|
74
|
|
|
describe(`[method] tests`, function() { |
|
75
|
|
|
let sandbox; |
|
76
|
|
|
beforeEach(function() { |
|
77
|
|
|
sandbox = sinon.sandbox.create(); |
|
78
|
|
|
}); |
|
79
|
|
|
afterEach(function() { |
|
80
|
|
|
sandbox.restore(); |
|
81
|
|
|
}); |
|
82
|
|
|
it(`should yield a result for each of the 3 tests`, async function() { |
|
83
|
|
|
let parameters = { |
|
84
|
|
|
"exercise": `aaa`, |
|
85
|
|
|
"tests": [1, 2, 3], |
|
86
|
|
|
"language": `Python`, |
|
87
|
|
|
"bundle": `print('Hello world!')` |
|
88
|
|
|
}; |
|
89
|
|
|
let test = sandbox.stub(cgapi, `test`, function() {return Promise.resolve(true)}); |
|
90
|
|
|
for await (let result of utils.tests(parameters)) { |
|
91
|
|
|
expect(result).to.have.be.ok; |
|
92
|
|
|
} |
|
93
|
|
|
expect(test).to.have.callCount(3); |
|
94
|
|
|
}); |
|
95
|
|
|
}); |
|
96
|
|
|
}); |
|
97
|
|
|
|